using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;
using System.Drawing;

namespace Utils.Imdb
{
    public class Tools
    {
        private static string url_webservice = "http://www.trynt.com/movie-imdb-api/v2/";

        private static string connectUrl(string url)
        {
            string response = "";

            WebClient webClient = new WebClient();
            response = webClient.DownloadString(url);

            return response;
        }   

        public static string getImdbInfoById(string id)
        {
            string response = "";
            try
            {
                string url = url_webservice + "?i=" + id + "&u=&fo=xml&f=0";
                response = connectUrl(url);                
            }
            catch (Exception ex)
            {
                // TODO: criar um log das mensagens de erro...
            }

            return response;
        }

        public static string getImdbInfoByTitle(string title)
        {
            string response = "";
            try
            {
                string url = url_webservice + "?t=" + title + "&u=&fo=xml&f=0";
                response = connectUrl(url);
            }
            catch (Exception ex)
            {
                // TODO: criar um log das mensagens de erro...
            }

            return response;
        }

        public static string getImdbId(string title)
        {
            try
            {
                //FIXME: it's necessary convert string title to utf8 encode
                string url = url_webservice + "?t=" + title + "&fo=xml&f=1";
                string rsp = connectUrl(url);

                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(rsp);

                XmlNode xmlnode = xmldoc.SelectSingleNode("/*/movie-imdb/data/movie-id/data/id");

                return xmlnode.FirstChild.Value;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static Bitmap LoadPicture(string url)
        {
            HttpWebRequest wreq;
            HttpWebResponse wresp;
            Stream mystream;
            Bitmap bmp;

            bmp = null;
            mystream = null;
            wresp = null;
            try
            {
                wreq = (HttpWebRequest)WebRequest.Create(url);
                wreq.AllowWriteStreamBuffering = true;

                wresp = (HttpWebResponse)wreq.GetResponse();

                if ((mystream = wresp.GetResponseStream()) != null)
                    bmp = new Bitmap(mystream);
            }
            finally
            {
                if (mystream != null)
                    mystream.Close();

                if (wresp != null)
                    wresp.Close();
            }

            return (bmp);
        }
    }
}
